<?php

namespace App\Http\Middleware;

use App\Models\Node;
use Closure;

class AminLoginMiddleware
{
    /**
     * Handle an incoming request.
     *后台是否登录验证
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (!auth()->check()){
         return redirect(route('admin.login'))->withErrors(['error'=>'请登录']);
        }
        //menu
        $menuData=(new Node())->treeData(array_keys(session('admin.auth')));
        view()->share('menuData', $menuData);
        //当前路由别名
        $rootrole=$request->route()->getName();
        //去空
       $role= array_filter(session('admin.auth'));
       //合并
       $role=array_merge($role,config('web.allow_node'));
       if (auth()->id()!=config('web.Administrators') && !in_array($rootrole,$role)){
           exit( '你没有权限');
       }

        //权限判断
        return $next($request);
    }
}
